﻿<template>
  <a-card :bordered="false" :title="showTitle">
    <div class="table-operator">
      <a-button v-if="hasPerm('Base_GeneralConfig.Add')" type="primary" icon="plus" @click="handleAdd()">新建</a-button>
      <a-button
        v-if="hasPerm('Base_GeneralConfig.Delete')"
        type="primary"
        icon="minus"
        @click="handleDelete(selectedRowKeys)"
        :disabled="!hasSelected()"
        :loading="loading"
      >删除
      </a-button>
      <a-button type="primary" icon="redo" @click="getDataListByHostId()">刷新</a-button>
    </div>

    <!--    <div class="table-page-search-wrapper">-->
    <!--      <a-form layout="inline">-->
    <!--        <a-row :gutter="10">-->
    <!--          <a-col :md="4" :sm="24">-->
    <!--            <a-form-item label="查询类别">-->
    <!--              <a-select allowClear v-model="queryParam.condition">-->
    <!--                <a-select-option key="Code">自定义编码</a-select-option>-->
    <!--                <a-select-option key="HostTableName">主表名称</a-select-option>-->
    <!--                <a-select-option key="HostId">主表ID</a-select-option>-->
    <!--                <a-select-option key="SlaveTableName">从表名称</a-select-option>-->
    <!--                <a-select-option key="SlaveId">从表ID</a-select-option>-->
    <!--                <a-select-option key="Remark">备注</a-select-option>-->
    <!--              </a-select>-->
    <!--            </a-form-item>-->
    <!--          </a-col>-->
    <!--          <a-col :md="4" :sm="24">-->
    <!--            <a-form-item>-->
    <!--              <a-input v-model="queryParam.keyword" placeholder="关键字" />-->
    <!--            </a-form-item>-->
    <!--          </a-col>-->
    <!--          <a-col :md="6" :sm="24">-->
    <!--            <a-button type="primary" @click="() => {this.pagination.current = 1; this.getDataList()}">查询</a-button>-->
    <!--            <a-button style="margin-left: 8px" @click="() => (queryParam = {})">重置</a-button>-->
    <!--          </a-col>-->
    <!--        </a-row>-->
    <!--      </a-form>-->
    <!--    </div>-->

    <a-table
      ref="table"
      :scroll="{y:530}"
      :columns="columns"
      :rowKey="row => row.Id"
      :dataSource="data"
      :pagination="pagination"
      :loading="loading"
      @change="handleTableChange"
      :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
      :bordered="true"
      size="small"
    >
      <span slot="action" slot-scope="text, record">
        <template>
          <a v-if="hasPerm('Base_GeneralConfig.Edit')" @click="handleEdit(record.Id)">编辑</a>
          <a-divider v-if="hasPerm('Base_GeneralConfig.Edit')&&hasPerm('Base_GeneralConfig.Delete')" type="vertical" />
          <a v-if="hasPerm('Base_GeneralConfig.Delete')" @click="handleDelete([record.Id])">删除</a>
        </template>
      </span>
    </a-table>
    <edit-form ref="editForm" :parentObj="this"></edit-form>
  </a-card>
</template>

<script>
import EditForm from './EditForm'

const columns = [
  { title: '排序', dataIndex: 'Sort' },
  { title: '主表名称', dataIndex: 'HostTableName' },
  { title: '主表ID', dataIndex: 'HostId' },
  { title: '从表名称', dataIndex: 'SlaveTableName' },
  { title: '从表ID', dataIndex: 'SlaveId' },
  { title: '备注', dataIndex: 'Remark' },
  { title: '操作', dataIndex: 'action', scopedSlots: { customRender: 'action' } }
]

export default {
  components: {
    EditForm
  },
  props: {
    hostTableName: { type: String, required: false }, // 主表名称
    slaveTableName: { type: String, required: false }, // 从表名称
    hostTitle: { type: String, required: false }, // 主表编辑下拉框标题
    slaveTitle: { type: String, required: false } // 从表编辑下拉框标题
  },
  mounted () {
    this.getDataListByHostId()
  },
  data () {
    return {
      data: [],
      pagination: {
        current: 1,
        pageSize: 20,
        showTotal: (total, range) => `总数:${total} 当前:${range[0]}-${range[1]}`,
        pageSizeOptions: ['10', '20', '30', '40', '50', '100'], // 设置pageSize的可选值，页面啊可以通过下拉框进行选择
        showSizeChanger: true
      },
      filters: {},
      sorter: { field: 'Id', order: 'desc' },
      loading: false,
      columns,
      isAppoint: false, // 判断是否查询全部
      showTitle: '', // 标题
      editFormTitle: '',
      hostId: '', // 主表ID
      queryParam: {},
      selectedRowKeys: []
    }
  },
  methods: {
    handleTableChange (pagination, filters, sorter) {
      this.pagination = { ...pagination }
      this.filters = { ...filters }
      this.sorter = { ...sorter }
      this.getDataListByHostId()
    },
    getDataList () {
      this.selectedRowKeys = []
      this.loading = true
      this.$http
        .post('/Base_BasicData/Base_GeneralConfig/GetDataList', {
          PageIndex: this.pagination.current,
          PageRows: this.pagination.pageSize,
          SortField: this.sorter.field || 'Id',
          SortType: this.sorter.order || 'desc',
          Search: this.queryParam,
          ...this.filters
        })
        .then(resJson => {
          this.loading = false
          this.data = resJson.Data
          if (this.data) {
            const pagination = { ...this.pagination }
            pagination.total = resJson.Total
            this.pagination = pagination
          }
        })
    },
    getDataListByHostId () {
      this.selectedRowKeys = []
      this.loading = true
      this.queryParam = {
        HostId: this.hostId,
        HostTableName: this.hostTableName,
        SlaveTableName: this.slaveTableName
      }
      console.log(this.queryParam)
      this.$http
        .post('/Base_BasicData/Base_GeneralConfig/GetDataListByHostId', {
          PageIndex: this.pagination.current,
          PageRows: this.pagination.pageSize,
          SortField: this.sorter.field || 'Id',
          SortType: this.sorter.order || 'desc',
          Search: this.queryParam,
          ...this.filters
        })
        .then(resJson => {
          this.loading = false
          this.data = resJson.Data
          if (this.data) {
            const pagination = { ...this.pagination }
            pagination.total = resJson.Total
            this.pagination = pagination
          }
        })
    },
    onSelectChange (selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys
    },
    hasSelected () {
      return this.selectedRowKeys.length > 0
    },
    handleAdd () {
      this.$refs.editForm.openForm(null, this.showTitle + ' > 新建' + this.editFormTitle)
    },
    handleEdit (id) {
      this.$refs.editForm.openForm(id, this.showTitle + ' > 编辑' + this.editFormTitle)
    },
    handleDelete (ids) {
      var thisObj = this
      this.$confirm({
        title: '确认删除吗?',
        onOk () {
          return new Promise((resolve, reject) => {
            thisObj.$http.post('/Base_BasicData/Base_GeneralConfig/DeleteData', ids).then(resJson => {
              resolve()
              if (resJson.Success) {
                thisObj.$message.success('操作成功!')
                thisObj.getDataListByHostId()
              } else {
                thisObj.$message.error(resJson.Msg)
              }
            })
          })
        }
      })
    }
  }
}
</script>
